using UnityEngine;
using System.Collections;

[RequireComponent (typeof (CharacterController))]//the character controller
[RequireComponent (typeof (tk2dAnimatedSprite))] //the sprite

public class MoveableSprite : AudioSprite {
	
	//the base movement speed that feels good
	protected const float MOVEMENT_SPEED = 1000f;
	
	//the sprite's movement speed
	protected float movementSpeed;
	
	//the character controller
	private CharacterController controller;
	
	private Vector3 moveDirection;
	
	//Axis of movement information
	protected float xMovement;
	protected float yMovement;
	
	protected tk2dAnimatedSprite sprite;
	
	// Start the DepthManager and this class
	override protected void Start () {
		base.Start();
		movementSpeed = MOVEMENT_SPEED;
		controller = GetComponent<CharacterController>();
		sprite = GetComponent<tk2dAnimatedSprite>();
		moveDirection = Vector3.zero;		
	}
	
	//handle the sprite's movement
	protected void handleMovement() 
	{
		moveDirection = new Vector3(xMovement, yMovement, 0);
       	moveDirection *= movementSpeed;
		controller.Move(moveDirection * Time.smoothDeltaTime);
		updateDepthPosition();//set this sprite's z-depth the the appropriate value for draw order
	}
	
	//update the sprite's movement if it's in motion
	override protected void Update() {
		if (xMovement != 0 || yMovement != 0) handleMovement();//only if in motion
		base.Update();
	}
	
	
}
